//
//  ImgCollectionViewController.swift
//  DynamicStateLJZ
//
//  Created by kys-11 on 2019/9/14.
//  Copyright © 2019年 xl123. All rights reserved.
//
    
//        let layout = UICollectionViewFlowLayout()
//        layout.itemSize = CGSize(width: UIScreen.main.bounds.width-20 , height: UIScreen.main.bounds.height-20)
//        layout.minimumLineSpacing = 15.0
//        layout.minimumInteritemSpacing = 0
//        layout.scrollDirection = .horizontal
import UIKit

private let reuseIdentifier = "Cell"
private let cellMargin: CGFloat = 15.0
class ImgCollectionViewController: UICollectionViewController {

    var currentIndexPath: IndexPath?
    var imageArray:[UIImage] = []
    
    override func viewDidLoad() {
        super.viewDidLoad()


        
        self.collectionView!.register(DYCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)

                collectionView?.frame.size.width = UIScreen.main.bounds.size.width + cellMargin  //重设宽度，翻页令图片处在正中
                self.collectionView?.isPagingEnabled = true
        
                guard currentIndexPath != nil else {
                    return
                }
                // 设置显示时的位置（默认是从[0,0]开始的）
                self.collectionView?.scrollToItem(at: currentIndexPath! , at: .left, animated: false)
  
    }

    convenience init() {
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = CGSize(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
        layout.minimumLineSpacing = 15.0
        layout.minimumInteritemSpacing = 0
        layout.scrollDirection = .horizontal
        self.init(collectionViewLayout: layout)

    }
    
override init(collectionViewLayout layout: UICollectionViewLayout){
    
     super.init(collectionViewLayout: layout)
    
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func numberOfSections(in collectionView: UICollectionView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }


    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return imageArray.count
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! DYCollectionViewCell
    
     
        guard  let image:UIImage = imageArray[indexPath.item] else {
        cell.dyimageview.image = imageArray[indexPath.row]
            return cell
        }
         cell.dyimageview.image = image
    
        return cell
    }

    
  
    override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        dismiss(animated: true, completion: nil)
    }
    
}
